# -*- coding: utf-8 -*-
'''
Created on 29-11-2012

@author: LONG HOANG GIANG
'''
from lxml import etree
from BeautifulSoup import BeautifulSoup
import cStringIO as StringIO
import gzip
import traceback

def convertHTMLEntitiesToUnicode(html):
    soup = BeautifulSoup(html, convertEntities=BeautifulSoup.HTML_ENTITIES)
    return unicode(soup.body or soup).encode('utf-8')

class HttpResponse():
    
    __html = ''
    __cookie = ''
    __info = ''
    
    def __init__(self, res):
        if res != None:
            self.__info = res.info()
            self.__cookie = self.__info.get('Set-Cookie', '')
            html = res.read()
            if self.__info.get('Content-Encoding', '') == 'gzip': html = self.decompress_gzip_html(html)
            if len(html) > 0:
                while(ord(html[0])>128 or ord(html[0])<32): 
                    html = html[1:]
            self.__html = html
    
    def decompress_gzip_html(self, source):
        gzipper = gzip.GzipFile(fileobj=StringIO.StringIO(source))
        data = gzipper.read()
        return data
    
    def gethtml(self, *arg, **kw):
        '''
        @param clear_entity: convert html entity to unicode, default value is False 
        '''
        clear_entity = kw.get('clear_entity', False)
        if len(arg) > 0:
            clear_entity = arg[0]
        return self.__html if clear_entity else convertHTMLEntitiesToUnicode(self.__html)
    
    def getcookie(self):
        return self.__cookie
    
    def getheader(self):
        return self.__info
    
    def build_tree(self, **kw):
        '''
        @param scheme: True if document is xml document
        @param base_url: default value is None 
        '''
        tree = None
        parser = etree.HTMLParser(encoding='utf-8') if kw.get('scheme', 'html') == 'html' else etree.XMLParser(encoding='utf-8')
        try:
            tree = etree.parse(StringIO.StringIO(self.__html), parser=parser, base_url=kw.get('base_url', None))
        except:
            traceback.print_exc()
        return tree
    
    
    
    